﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Diagnostics;

/// <summary>
/// Summary description for Restaurant
/// </summary>
namespace Entities
{
    public class Restaurant
    {
        public Restaurant(Int16 restaurantId)
        {
            Id = restaurantId;
        }
        public Restaurant()
        {
            _id = 0;
        }

        private Int16 _id;
        private String _name;
        private String _tel;
        private String _mobile;
        private String _advertise;
        private String _begin;
        private String _end;
        private Boolean _open;
        private Boolean _aproved;
        private Boolean _online;
        private Boolean _selfM;
        private DateTime _deadline;
        public Score Score;
        public Owner Owner;
        public Address Address;
        public Delivery Delivery;

        public void SetScore(Int16 scoreId)
        {
            Debug.Assert(Score == null);

            BLL.Command.SetScoreForRestaurant(_id, scoreId);
            this.Score = new Score(scoreId);
        }
        public Int16 ScoreId
        {
            get { return Score.Id;}
        }
        public Int16 ScoreGood
        {
            get { return Score.Good;}
        }
        public Int16 ScoreMiddle
        {
            get { return Score.Middle; }
        }
        public Int16 ScoreBad
        {
            get { return Score.Bad; }
        }

        public Int16 Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public String Tel
        {
            get { return _tel; }
            set { _tel = value; }
        }
        public String Mobile
        {
            get { return _mobile; }
            set { _mobile = value; }
        }
        public String Advertise
        {
            get { return _advertise; }
            set { _advertise = value; }
        }
        public String Begin
        {
            get { return _begin; }
            set { _begin = value; }
        }
        public String End
        {
            get { return _end; }
            set { _end = value; }
        }
        public Boolean Open
        {
            get { return _open; }
            set { _open = value; }
        }
        public Boolean Aproved
        {
            get { return _aproved; }
            set { _aproved = value; }
        }
        public Boolean Online
        {
            get { return _online; }
            set { _online = value; }
        }
        public Boolean SelfM
        {
            get { return _selfM; }
            set { _selfM = value; }
        }
        public DateTime Deadline
        {
            get { return _deadline; }
            set { _deadline = value; }
        }
        public String AddressDescription
        {
            get { return Address.Description; }
        }
        public String DeliveryDescription
        {
            get { return Delivery.Description; }
        }

        private Menu _menu;
        public Menu Menu
        {
            get {
                    if (_menu == null)
                    {
                        _menu = new Menu(Id);
                    }
                    return _menu;
            }
        }
        private List<District> _servedDistricts;
        public List<District> ServedDistricts
        {
            get
            {
                if (_servedDistricts == null)
                {
                    _servedDistricts = BLL.Command.GetServedDistricts(Id);
                }
                return _servedDistricts;
            }
            set { _servedDistricts = value; }
        }
        
        public bool DoesServe(Int16 districtId)
        {
            foreach (District district in ServedDistricts)
            {
                if (district.Id == districtId)
                    return true;
            }
            return false;
        }
    }

    public class Score
    {
        public Score()
        { }
        public Score(Int16 id)
        {
            Id = id;
        }
        public Int16 Id;
        public Int16 Good;
        public Int16 Middle;
        public Int16 Bad;
        public Int16 ParentId;
    }
    public class Address
    {
        public Address()
        { 
        }
        public Int16 Id;
        public String Road;
        public Int16 Building;
        public Int16 Room;
        public String Description;
        public District District;
    }
    public class Owner
    {
        public Owner()
        { }
        public Int16 Id;
        public String Name;
        public String Pwd;
        public String Phone;
        public String IdCard;
    }
    public class District
    {
        public District(Int16 id)
        {
            Id = id;
            TopNum = 20;
        }
        private Int16 _id;
        private String _name;
        private String _description;
        private Byte _type;
        List<Restaurant> _restaurantsOfDistrict;
        public Int16 TopNum;

        public Int16 Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public String Description
        {
            get { return _description; }
            set { _description = value; }
        }
        public Byte Type
        {
            get { return _type; }
            set { _type = value; }
        }
        public Int16 ParentId;
        public List<Restaurant> RestaurantsOfDistrict
        {
            get
            {
                if (null == _restaurantsOfDistrict)
                    _restaurantsOfDistrict = BLL.Command.GetRestaurantsByDistrict(Id, TopNum);

                return _restaurantsOfDistrict;
            }
            set { _restaurantsOfDistrict = value; }
        }
    }
    public class Delivery
    {
        public Delivery()
        { }
        public Int16 Id;
        public String Description;
    }
    public class Menu
    {
        public Menu(Int16 restaurantId)
        {
            RestaurantId = restaurantId;
        }
        public Int16 RestaurantId;
        private List<Category> _categories;
        public List<Category> Categories
        {
            get
            {
                if (_categories == null)
                {
                    _categories = new List<Category>();
                    _categories = BLL.Command.GetCategoriesByRestaurant(RestaurantId);
                }
                return _categories;
            }
        }
        public DataSet AllDishesDataReader
        {
            get { return BLL.Command.GetDishesByRestaurant(RestaurantId); }
        }
    }
}